﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Collections;
using System.IO;

namespace GraphGeneration
{  
    
    public partial class Form1 : Form
    {

        bool debugMode = false;


        bool EditedAuto = false;

        public Form1()
        {
            InitializeComponent();
           
        }

        Generator gen;

        private void button1_Click(object sender, EventArgs e)
        {
            debugBox.Text += "Rerolling weights... ";
            gen.RerollWeights((int)nudMinWeight.Value, (int)nudMaxWeight.Value, cbUndirectional.Checked);
            debugBox.Text += "Done!\nSaving... ";

            if (debugMode)
            {

                foreach (var G in gen.PrintGraph())
                {
                    debugBox.Text += G.ToString() + " \n";
                }
            }
            else
                Save();
            
            debugBox.Text += "Done!\n";
        }

        private void bGenerateGraph_Click(object sender, EventArgs e)
        {
            generate(true);              

        }

        private void Save()
        {

            SaveFileDialog saveFile = new SaveFileDialog();
            saveFile.ShowDialog();

            if (saveFile.FileName != "")
            {
                System.IO.FileStream fs = (System.IO.FileStream)saveFile.OpenFile();
                StreamWriter sr = new StreamWriter(fs);

                foreach (var G in gen.PrintGraph())
                {
                    sr.WriteLine(G.ToString());
                }

                sr.Close();

            }

        }

        private void Save(String str)
        {

            System.IO.FileStream fs = new FileStream(str, FileMode.Create);
            StreamWriter sr = new StreamWriter(fs);

            foreach (var G in gen.PrintGraph())
            {
                sr.WriteLine(G.ToString());
            }

            sr.Close();

        }

        private void checkBox1_CheckedChanged(object sender, EventArgs e)
        {

        }

        private void nudEdges_ValueChanged(object sender, EventArgs e)
        {

            if (!EditedAuto)
            {
                EditedAuto = true;
                long maxEdges = ((long)nudVertex.Value * ((int)nudVertex.Value - 1));

                if (cbUndirectional.Checked)
                    maxEdges /= 2;

                if (nudEdges.Value > maxEdges)
                    nudEdges.Value = maxEdges;

                if (nudEdges.Value <= nudVertex.Value)
                    nudEdges.Value = nudVertex.Value;

                double val;
                val = (int)nudEdges.Value * 100 / maxEdges;

                nudEdgesPercent.Value = (decimal)val;
                
            }
            else
            EditedAuto = false;
        }

        private void nudEdgesPercent_ValueChanged(object sender, EventArgs e)
        {
            if (!EditedAuto)
            {
                EditedAuto = true;
                long maxEdges = ((long)nudVertex.Value * ((int)nudVertex.Value - 1));

                if (cbUndirectional.Checked)
                    maxEdges /= 2;

                if (nudEdgesPercent.Value >= 100)
                    nudEdgesPercent.Value = 100;

                if (nudEdges.Value <= nudVertex.Value)
                    nudEdges.Value = nudVertex.Value;

                double val;
                val = (double)nudEdgesPercent.Value / 100 * (int)maxEdges;
                if (val < 1)
                    val = 1;
                nudEdges.Value = (int)val;
                
            }
            else
                EditedAuto = false;
        }

        private void nudVertex_ValueChanged(object sender, EventArgs e)
        {
            nudEdgesPercent_ValueChanged(sender, e);
        }

        private void btMakeSet_Click(object sender, EventArgs e)
        {


            int[] complet = new int[] { 20, 50, 100, 250, 500, 1000, 2500, 5000 };

            int[] percents = new int[]{5, 10, 25, 50, 75, 90, 100};
           

            String path;
            String test; 
            String dir;
            String file;

            FolderBrowserDialog fbr = new FolderBrowserDialog();
            if (fbr.ShowDialog() == DialogResult.OK)
            {
                path = fbr.SelectedPath;

                for (int k = 0; k < complet.Count(); k++)
                {

                    debugBox.Text += "Doing test " + k + " of " + complet.Count() + '\n'; 

                    test = path + '\\' + complet[k].ToString();
                    Directory.CreateDirectory(test);

                    nudVertex.Value = complet[k];

                    for (int j = 0; j < percents.Count(); j++)
                    {
                        dir = test + '\\' + percents[j].ToString();
                        Directory.CreateDirectory(dir);
                        nudEdgesPercent.Value = percents[j];

                        generate(false);

                        for (int i = 1; i <= 5; i++)
                        {

                            gen.RerollWeights((int)nudMinWeight.Value, (int)nudMaxWeight.Value, cbUndirectional.Checked);
                            file = dir + '\\' + Math.Round(nudEdgesPercent.Value, 0).ToString() + '-' + i.ToString();
                            Save(file);
                        }

                    }

                }
            }


        }


        void generate(bool save)
        {
            
            gen = new Generator();
            //debugBox.Text = "";

            if (nudEdges.Value < nudVertex.Value) //Minimalna liczba krawedzi
                nudEdges.Value = nudVertex.Value;

            long maxEdges = ((long)nudVertex.Value * ((int)nudVertex.Value - 1));

            if (cbUndirectional.Checked)         //Maksymalna liczba krawedzi
                maxEdges /= 2;

            if (nudEdges.Value > maxEdges)
                nudEdges.Value = maxEdges;

            if (nudMaxWeight.Value < nudMinWeight.Value)
                nudMaxWeight.Value = nudMinWeight.Value;

            debugBox.Text += "Creating graph: V=" + (int)nudVertex.Value + " E=" + (int)nudEdges.Value + " Min=" + (int)nudMinWeight.Value + " Max=" + (int)nudMaxWeight.Value + " ";

            gen.GenerateGraph((int)nudVertex.Value, (int)nudEdges.Value, cbUndirectional.Checked, (int)nudMinWeight.Value, (int)nudMaxWeight.Value);

            debugBox.Text += "Created! Saving...";

            if (debugMode)
            {

                foreach (var G in gen.PrintGraph())
                {
                    debugBox.Text += G.ToString() + " \n";
                }
            }
            
            if(save)
                Save();

            debugBox.Text += "Done! \n";
                  

        }

        private void button1_Click_1(object sender, EventArgs e)
        {

            int[] percents = new int[] { 5, 10, 25, 50, 75, 90, 100 };


            String path;    
            String dir;
            String file;

            FolderBrowserDialog fbr = new FolderBrowserDialog();
            if (fbr.ShowDialog() == DialogResult.OK)
            {
                    path = fbr.SelectedPath;


                    for (int j = 0; j < percents.Count(); j++)
                    {
                        dir = path + '\\' + percents[j].ToString();
                        Directory.CreateDirectory(dir);
                        nudEdgesPercent.Value = percents[j];

                        generate(false);

                        for (int i = 1; i <= 5; i++)
                        {

                            gen.RerollWeights((int)nudMinWeight.Value, (int)nudMaxWeight.Value, cbUndirectional.Checked);
                            file = dir + '\\' + Math.Round(nudEdgesPercent.Value, 0).ToString() + '-' + i.ToString();
                            Save(file);
                        }

                    

                }
            }
        }


    }
}
